package yagwl.service.demo;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import yagwl.service.util.CryptoUtil;

@SuppressWarnings("serial")
@Entity
@Table(name = "PRODUCT_CATEGORY")
public class ProductCategoryEntity implements Serializable {

  private static final String entityCode = "PCAT";

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "PRODUCT_CATEGORY_ID")
  private Long productCategoryId;

  /*
   * Google BigTable doesn't support long primary keys for owned relationships.
   * Hence manually handling parent-child relationships. Second, there is a data
   * nucleus bug that doesn't allow null parent The root will have always have a
   * null parent (10.04.10)
   */

  @Column(name = "PARENT_ID")
  private Long parentProductCategoryId;

  // @OneToMany(mappedBy = "parent")
  // private List<ProductCategoryEntity> children;

  // @ManyToOne
  // @JoinColumn(name = "PARENT_ID", nullable = true)
  // private ProductCategoryEntity parent;

  /*
   * ManyToMany not currently supported on App Engine, but it allows collection
   * of keys; note that this wont work in other relational databases
   */

  @Column(name = "PRODUCT_IDS")
  private List<Long> productIds;

  // @ManyToMany
  // @JoinTable(name = "CATEGORY_PRODUCT", //
  // joinColumns = @JoinColumn(name = "CATEGORY_ID"), //
  // inverseJoinColumns = @JoinColumn(name = "PRODUCT_ID"))
  // private List<ProductEntity> products;

  @Column(name = "NAME")
  private String name;

  @Column(name = "IMAGE_URL")
  private String imageUrl;

  @Transient
  private List<ProductCategoryEntity> children;

  @Transient
  private List<ProductEntity> products;

  Long getProductCategoryId() {
    return productCategoryId;
  }

  void setProductCategoryId(Long id) {
    this.productCategoryId = id;
  }

  public String getEncryptedProductCategoryId() {
    return encryptId(productCategoryId);
  }

  public void setEncryptedProductCategoryId(String id) {
    productCategoryId = decryptId(id);
  }

  static String encryptId(Long id) {
    return CryptoUtil.encryptLong(id, entityCode);
  }

  static Long decryptId(String id) {
    return CryptoUtil.decryptLong(id, entityCode);
  }

  Long getParentProductCategoryId() {
    return parentProductCategoryId;
  }

  void setParentProductCategoryId(Long parentId) {
    this.parentProductCategoryId = parentId;
  }

  public String getEncryptedParentProductCategoryId() {
    if (parentProductCategoryId == null)
      return null;
    return encryptId(parentProductCategoryId);
  }

  public void setEncryptedParentProductCategoryId(String parentId) {
    this.parentProductCategoryId = decryptId(parentId);
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
  }

  public String getImageUrl() {
    return imageUrl;
  }

  public void setChildren(List<ProductCategoryEntity> children) {
    this.children = children;
  }

  public List<ProductCategoryEntity> getChildren() {
    return children;
  }

  public void setProducts(List<ProductEntity> products) {
    this.products = products;
  }

  public List<ProductEntity> getProducts() {
    return products;
  }

  public void setProductIds(List<Long> productIds) {
    this.productIds = productIds;
  }

  public List<Long> getProductIds() {
    return productIds;
  }
}
